/**
 * 
 */
package com.wolfpire.proms.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wolfpire.proms.model.Task;
import com.wolfpire.proms.service.TaskService;
import com.wolfpire.proms.vo.PersonalCountVo;
import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.Result;
import com.wolfpire.system.common.springmvc.PrincipalArgument;
import com.wolfpire.system.model.Authority;
import com.wolfpire.system.model.User;
import com.wolfpire.system.service.AuthorityService;

/**
 * 混合的一些个人信息控制器
 * @author lihd
 *
 */

@Controller
@RequestMapping("/personInfo")
public class PersonalInfoController {
	
	@Resource private TaskService taskService;
	
	@Resource private AuthorityService authorityService;
	
	@RequestMapping("/todoCount")
	public ModelAndView todoCount() {
		return new ModelAndView("proms/personInfo/todo_count");
	}
	
	@ResponseBody
	@RequestMapping(value = "/todoCountData", method = {RequestMethod.GET, RequestMethod.POST})
	public Result todoCountData(@PrincipalArgument User user) {
		try {
			Result result = new Result();
			List<PersonalCountVo> lists = new ArrayList<PersonalCountVo>();
			// 任务待办
			Task filterTask = new Task();
			filterTask.setCreateUserId(user.getId());
			filterTask.setDelFlag(Constants.NORMAL_FLAG);
			// 待办数
			int todoTaskCount = this.taskService.countTodoTasks(filterTask);
			if (0 < todoTaskCount) {
				// 待办菜单id
				String todoTaskMenuId = "45";
				Authority authority = this.authorityService.get(Long.parseLong(todoTaskMenuId));
				PersonalCountVo pcv = new PersonalCountVo("todoTask", todoTaskMenuId, todoTaskCount, authority.getName(), authority.getPath());
				lists.add(pcv);
			}
			boolean success = CollectionUtils.isNotEmpty(lists);
			result.setSuccess(success);
			if (success) {
				result.setData(lists);
			}
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false);
		}
	}
}
